const int nLIGHTS = 9;

const float kAmbient = 0.2;
const vec4 ambient = vec4(kAmbient, kAmbient, kAmbient, 1.0);

const float kGlobalAmbient = 0.2;
const vec4 globalAmbient = vec4(kGlobalAmbient, kGlobalAmbient, kGlobalAmbient, 1.0);

uniform sampler2D texture;
uniform vec3 lightPos[nLIGHTS];

varying float kDiffuse[nLIGHTS];
varying float kFalloff[nLIGHTS];

void main() {
    vec4 texel = texture2D(texture, gl_TexCoord[0].st);
    
    ///*
    vec4 lightSum = vec4(0.0, 0.0, 0.0, 0.0);
    
    float localDiffuse;
    vec4 localSum;
    for (int i = 0; i < 2; i++) {
    	lightSum.x += kFalloff[i] * (kDiffuse[i] + ambient.r);
    	lightSum.y += kFalloff[i] * (kDiffuse[i] + ambient.g);
    	lightSum.z += kFalloff[i] * (kDiffuse[i] + ambient.b);
    	lightSum.w += ambient.a;
    }
    //*/
    /*
    vec4 diffuse0 = vec4(kFalloff[0] * vec3(kDiffuse[0], kDiffuse[0], kDiffuse[0]), 1.0);
    vec4 ambient0 = vec4(kFalloff[0] * ambient.rgb, ambient.a);
    */
    
    //vec4 lightColorLim = clamp(globalAmbient + diffuse0 + ambient0, 0.0, 1.0);
    vec4 lightColorLim = clamp(globalAmbient + lightSum, 0.0, 1.0);
    
    vec3 finalColor3 = texel.rgb * lightColorLim.rgb;
    float finalAlpha = texel.a   * lightColorLim.a  ;
    
    vec4 finalColor4 = vec4(finalColor3, finalAlpha);
    gl_FragColor = clamp(finalColor4, 0.0, 1.0);
}
